using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Inventory.Domain.Models;
using Inventory.Database;

namespace Inventory.Database.Models
{ 
    public class PersonsProfessionRepository : IPersonsProfessionRepository
    {
        readonly InventoryContext _context = new InventoryContext();

        public PersonsProfessionRepository(UnitOfWork unitOfWork)
        {
            _context = unitOfWork.Context;
        }

        public IQueryable<PersonsProfession> All
        {
            get { return _context.PersonsProfessions; }
        }

        public IQueryable<PersonsProfession> AllIncluding(params Expression<Func<PersonsProfession, object>>[] includeProperties)
        {
            IQueryable<PersonsProfession> query = _context.PersonsProfessions;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public PersonsProfession Find(int id)
        {
            return _context.PersonsProfessions.Find(id);
        }

        public void InsertOrUpdate(PersonsProfession personsprofession)
        {
            if (personsprofession.Id == default(int)) {
                // New entity
                _context.PersonsProfessions.Add(personsprofession);
            } else {
                // Existing entity
                _context.Entry(personsprofession).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var personsprofession = _context.PersonsProfessions.Find(id);
            _context.PersonsProfessions.Remove(personsprofession);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IPersonsProfessionRepository : IDisposable
    {
        IQueryable<PersonsProfession> All { get; }
        IQueryable<PersonsProfession> AllIncluding(params Expression<Func<PersonsProfession, object>>[] includeProperties);
        PersonsProfession Find(int id);
        void InsertOrUpdate(PersonsProfession personsprofession);
        void Delete(int id);
        void Save();
    }
}